<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<link rel = "stylesheet" type="text/css" href = "layui/css/layui.css"></link>
<script src = "layui/layui.js"></script>
<script src="layui/jquery-3.3.1.min.js"></script>
</head>
<style>
    body{margin: 10px;}
    .demo-carousel{height: 200px; line-height: 200px; text-align: center;}
  </style>

<body class="layui-view-body">
<table class="layui-hide" id="test" lay-filter="test"></table>
 <div id="saveDiv" style="display: none"> 
 <form class="layui-form"  lay-filter="example">
  <div class="layui-form-item">
    <label class="layui-form-label">名称</label>
    <div class="layui-input-inline">
      <input type="text" name="title" id="adress" required  lay-verify="required" placeholder="请输入名称" autocomplete="off" class="layui-input">
    </div>
  </div>
  <div class="layui-form-item">
    <label class="layui-form-label">地址</label>
    <div class="layui-input-inline">
      <input type="text" name="title" id="aname" required lay-verify="required" placeholder="请输入地址" autocomplete="off" class="layui-input">
    </div>
  </div>

   <div class="layui-form-item">
    <label class="layui-form-label">农场主</label>
    <div class="layui-input-inline">
      <input type="text" name="title" id="boss" required  lay-verify="required"  autocomplete="off" class="layui-input">
    </div>
  </div>
  
  <div class="layui-form-item">
    <label class="layui-form-label">农场类型</label>
    <div class="layui-input-block">
      <input type="radio" name="type"  value="私营" title="私营">
      <input type="radio" name="type" value="自营" title="自营" checked>
    </div>
  </div>
  
  <div class="layui-form-item">
    <div class="layui-input-block">
      <button class="layui-btn" lay-submit=""  lay-filter="btn1" >立即提交</button>
      <button type="reset" class="layui-btn layui-btn-primary">重置</button>
    </div>
  </div>
  </form>
  </div>
  <div id="editDiv" style="display: none">
					<form class="layui-form"  lay-filter="example">
					<div class="layui-form-item">
							<label class="layui-form-label" ></label>
							<div class="layui-input-inline">
							  <input type="hidden" name="farmer" id="farmer1" lay-verify="title" autocomplete="off" placeholder="请输入编号" class="layui-input">
							</div>
						  </div>
					
					
  <div class="layui-form-item">
    <label class="layui-form-label">名称</label>
    <div class="layui-input-inline">
      <input type="text" name="title" id="adress1" required  lay-verify="required" placeholder="请输入名称" autocomplete="off" class="layui-input">
    </div>
  </div>
  <div class="layui-form-item">
    <label class="layui-form-label">地址</label>
    <div class="layui-input-inline">
      <input type="text" name="title" id="aname1" required lay-verify="required" placeholder="请输入地址" autocomplete="off" class="layui-input">
    </div>
  </div>

   <div class="layui-form-item">
    <label class="layui-form-label">农场主</label>
    <div class="layui-input-inline">
      <input type="text" name="title" id="boss1" required  lay-verify="required"  autocomplete="off" class="layui-input">
    </div>
  </div>
  
  <div class="layui-form-item">
    <label class="layui-form-label">农场类型</label>
    <div class="layui-input-block">
      <input type="radio" name="type2" id="type2" value="私营" title="私营">
      <input type="radio" name="type2" id="type2" value="自营" title="自营" checked>
    </div>
  </div>
  
  <div class="layui-form-item">
    <div class="layui-input-block">
      <button class="layui-btn" lay-submit=""  lay-filter="btn2" >立即提交</button>
      <button type="reset" class="layui-btn layui-btn-primary">重置</button>
    </div>
  </div>
  </form>
	</div>
   <script type="text/html" id="toolbarDemo">
  <div class="layui-btn-container">
    
  </div>

 <div class="layui-form-item" >
        <div class="layui-input-inline">
          <!-- <input type="text"> -->
             <input type="text" placeholder="请输入" class="layui-input" style="height:32px;" id="query">
</div>
        <div class="layui-btn-container">
             <button class="layui-btn layui-btn-sm" onclick="setup();"><i class="layui-icon">&#xe615;</i>搜索</button>  
<button onclick="dianji()" class="layui-btn layui-btn-sm" lay-event="save"><i class="layui-icon">&#xe608;</i>添加</button> 
 </div>
     </div>
</script>

<script type="text/html" id="toolbarDemo">
  <div class="layui-btn-container">
    <button class="layui-btn layui-btn-sm" lay-event="getCheckData">获取选中行数据</button>
    <button class="layui-btn layui-btn-sm" lay-event="getCheckLength">获取选中数目</button>
    <button class="layui-btn layui-btn-sm" lay-event="isAll">验证是否全选</button>
  </div>
</script>

 
<script type="text/html" id="barDemo">
  <a class="layui-btn layui-btn-xs" lay-event="edit" ><i class="layui-icon">&#xe642;</i>编辑</a>
  <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon">&#xe640;</i>删除</a>
</script>
 
            
       
 
<script>
 

layui.use(['table', 'form', 'layedit', 'laydate'], function(){
	var table = layui.table,
	$ = layui.jquery,form=layui.form;
	table.render({
		elem: '#test'
	 ,url:'Spring/book/showall'
	 ,toolbar: '#toolbarDemo'
,title: '用户数据表'
,cols: [[
  {type: 'checkbox', fixed: 'left'}
  ,{field:'bno', title:'编号', width:150, fixed: 'left', unresize: true, sort: true}
  ,{field:'bname', title:'名称', width:200, edit: 'text'}
  ,{field:'author', title:'作者', width:80, edit: 'text'}
  ,{field:'suoyin', title:'索引', width:150, edit: 'text'}
  ,{field:'aa', title:'A', width:150, edit: 'text'}
  ,{field:'bb', title:'B', width:150, edit: 'text'}
  ,{field:'cc', title:'C', width:150, edit: 'text'}
  ,{field:'dd', title:'D', width:150, edit: 'text'}
  ,{fixed: 'right-', title:'操作', toolbar: '#barDemo', width:150}

    ]]
    ,page: true
  });
  
	//头工具栏事件
	  table.on('toolbar(test)', function(obj){
	    var checkStatus = table.checkStatus(obj.config.id);
	    switch(obj.event){
	      case 'save':
	        //添加弹出form
			layer.open({
	        type: 1,
			content: $("#saveDiv").append(),
	        id:"alertcenterdiv",
	        title: '信息处理',
	        area: ['400px', '320px'], //宽高
	        cancel: function (index, layero) {
	          layer.close(index);
	          $("#saveDiv").css("display", "none");
	          return false;
	        }       
	      });
		  break;
	    };
	  });
	  
	  //监听行工具事件
	  table.on('tool(test)', function(obj){
	    var data = obj.data;
	    //console.log(obj)
	    if(obj.event === 'del'){
	      layer.confirm('真的删除行么', function(index){
	        //ajax提交后台//自己补全
	        obj.del();
			
	        $.ajax({
	              url: "http://localhost:8080/nongchang/nongchang?type1=delpage",
	              type: "POST",
	              data: {farmer: data.farmer, adress: data.adress,aname: data.aname,boss: data.boss,type: data.type},
	              success: function (msg) {
	                  if (msg == "您的操作已成功") {
	                      //删除这一行
	                      alert("成功")
	                      //关闭弹框
	                      layer.close(index);
	                     // layer.msg("删除成功", {icon: 6});
	                      
	                  } else {
	                      layer.msg("删除成功", {icon: 5});
	                  }
	              }
	          });
	        
	        
	        
	        
	        layer.close(index);
	      });
	    } else if(obj.event === 'edit'){
			var data = obj.data; 
			var layEvent = obj.event; // 获得 lay-event 对应的值（也可以是表头的event 参数对应的值）
			var tr = obj.tr;         // 获得当前行 tr 的DOM对象
			
	alert(form.val('filter','object'))
	//修改弹出form
	layer.open({
		
    type: 1,
    shade:0,
	content: $("#editDiv").append(),
    id:"alertcenterdiv1",
    title: '信息处理',
    area: ['400px', '500px'], //宽高
    
    success : function(layero, index) {
    	
    	//console.log(layero, index);
    	//console.log(data.subno,data.subname);
    	//console.log(data.tsex);
    	
    	alert("编辑");
    	layer.iframeAuto(index);
		$('#farmer1').val(data.farmer);	
		$('#adress1').val(data.adress);//机会来源
		$('#aname1').val(data.aname);//创建人
		$('#boss1').val(data.boss);
		$("input[name='type2']:checked").val();
				
    }
	
  });
	

}
//Demo
		    layui.use('form', function(){
		      var form = layui.form;
		      
		      //监听提交
		      form.on('submit(formDemo)', function(data){
		        layer.msg(JSON.stringify(data.field));
		        return false;
		      });
		    });
		  });
	  
	  //form 事件处理的
	  form.on('submit(btn1)', function(data){
	    $.ajax({
			method:"post",
			data:{
					 
					adress:$("#adress").val()
					,aname:$("#aname").val()
					,boss:$("#boss").val()
					,type: $('input:radio:checked').val()
					
				},  		
			url:'/nongchang/nongchang?type1=save',
			success:function (data){
				if(data.code == 0){
					layer.close(index); //再执行关闭
				}else if(data.code != 0){
					window.location.href= "../view/nongchang.html" ;
					
				}
			}
		});
	    return false;
	  });

	  form.on('submit(btn2)', function(data){

		    $.ajax({
				method:"post",
				data:{
				 farmer:$("#farmer1").val()
			    ,adress:$("#adress1").val()
				,aname:$("#aname1").val()
				,boss:$("#boss1").val()
				,type:$("input[name='type2']:checked").val()
				
					},  
					
				url:'/nongchang/nongchang?type1=update',
				
				success:function (data){
					//alert($("#boss1").val());
		var json = eval('(" + data + ")')
		if(json){
			alert("成功");
			window.location.href= "../view/nongchang.html";					
		}else{
			alert("失败");
		}
	}
});
return false;
});

	  layui.use('form', function(){
		  var form = layui.form;
		  form.render();

	  
	});
	});
	
</script>


</body>
</html>